import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"


Vue.use(Vuex)


export default new Vuex.Store({
  state: {
    // 所有用户列表数据
    userInfo: [],
    // 登陆表单数据绑定对象
    login_form: {
      username: "",
      password: "",
    },
    // 图书列表数据
    bookList: [],
    // 表格数据
    tableList: [],
    // 图书表单数据
    bookForm: {
      Category: "",
      author: "",
      id: "",
      info: "",
      name: "",
      press: "",
      price: "",
    },
    // 书籍类型
    clazz: [],
    // 登陆的用户名
    logName: "",
    // 判断用户是不是超级管理员
    showOrHideBool: true,
    // 后台图片的路径
    uploadImg: '',
    // 登陆后的用户信息
    loginUser: {},
    // 所有用户
    userAll: [],
    // 图书数量 
    bookSum: "",
  },
  mutations: {
    // 给 login_form 赋值
    infoLoginForm(state, data) {
      state.login_form = data
    },
    // 给 bookList 赋值
    infoBook(state, book) {
      state.bookList = book
    },
    // 给 tableList 赋值
    infoTableList(state, table) {
      state.tableList = table
    },
    // 给 bookForm 赋值
    infoBookForm(state, book) {
      state.bookForm = book
    },
    // 给 clazz 赋值
    infoClazz(state, clazz) {
      state.clazz = clazz || []
    },
    // 给 logName 赋值
    infoLogName(state, e) {
      state.logName = e;
    },
    // 给 uploadImg 赋值
    infoUploadImg(state, e) {
      state.uploadImg = e
    },
    // 给loginUser 赋值
    infoLoginUser(state, e) {
      state.loginUser = e || {}
    },
    // 给 userAll 赋值
    infoUserAll(state, e) {
      state.userAll = e
    },
    // 给 bookSum 赋值 
    infoBookSum(state, e) {
      state.bookSum = e
    }
  },
  actions: {
    // 获取所有用户名 并赋值给 userInfo
    getUserList(ctx) {
      try {
        axios.get("/user").then(res => {
          ctx.commit("infoUserInfo", res.data.res.res);
        })
      } catch (e) { console.log(e) }
    },
    // 获取图书
    getBookList(ctx, book) {
      try {
        axios.get("/book", { params: book }).then(res => {
          ctx.commit("infoBook", res.data)
        })
      } catch (e) { console.log(e) }
    },
    // 获取数据统计数量
    getBookClazz(ctx, book) {
      try {
        axios.get("/book/getclazz", { params: book }).then(res => {
          ctx.commit("infoClazz", res.data)
          window.localStorage.setItem("clazz", JSON.stringify(res.data))
        })
      } catch (e) { console.log(e) }
    },
    // 搜索图书
    searchBook(ctx, e) {
      try {
        let params = {
          searchBook: e.book,
          currentPage: e.queryInfo.currentPage,
          pagesize: e.queryInfo.pagesize
        }
        axios.get("/book/searchbook", { params: params }).then(res => {
          ctx.commit("infoBook", res.data)
        })
      } catch (e) { console.log(e) }
    },
    // 添加书籍
    createBook(ctx, book) {
      try {
        axios.post("/book", { book: book }).then((res) => {
          console.log(res)
          ctx.dispatch("getBookList", book)
          ctx.dispatch("getBookClazz")
        }).catch(err => {
          console.log(err)
        })
      } catch (e) { console.log(e) }
    },
    // 编辑书籍
    putBook(ctx, e) {
      try {
        axios.put(`/book/${e.id}`, { book: e.book, cover: e.cover }).then(() => {
          ctx.dispatch("getBookList", e)
        }).catch(err => {
          err
        })
      } catch (e) { console.log(e) }
    },
    // 删除书籍
    removeBook(ctx, i) {
      try {
        axios.delete(`/book/${i.i}`).then(() => {
          ctx.dispatch("getBookList", i)
        }).catch(err => {
          err
        })
      } catch (e) { console.log(e) }
    },
    // 修改个人资料
    editPersonal(ctx, e) {
      try {
        let params = {
          username: e.username,
          userimg: e.userimg,
          nickname: e.nickname,
          mail: e.mail,
          phone: e.phone,
          sex: e.sex
        }
        axios.put(`/user/${e.username}`, { params: params }).then(res => {
          ctx.commit("infoLoginUser", res.data.res)
          let userJson = JSON.stringify(res.data.res[0]);
          window.localStorage.setItem("loginUser", userJson);
        })
      } catch (err) { err }
    },
    // 获取登录用户 资料
    getUsrInfo(ctx, e) {
      try {
        axios.get("/indexUser", { params: { username: e } }).then(res => {
          ctx.commit("infoLoginUser", res.data[0])
        })
      } catch (err) { err }
    },
    // 超级管理员获取所有用户信息
    getUserAll(ctx, e) {
      try {
        let loginUsername = window.localStorage.getItem("loginUsername");
        let params = {
          username: loginUsername,
          currentPage: e.currentPage,
          pagesize: e.pagesize
        }
        axios.get("/user", { params: params }).then(res => {
          ctx.commit("infoUserAll", res.data)
        })
      } catch (e) { console.log(e) }
    },
    // 向后台请求下拉菜单对应的数据
    getSelectList(ctx, e) {
      try {
        axios.get("/selectindex", { params: e }).then(res => {
          ctx.commit("infoBook", res.data)
        })
      } catch (e) { console.log(e) }
    }
  },
  getters: {
    // 加工 userInfo
    showUserInfo: state => {
      return state.userInfo
    },
    // 加工 login_form
    showLoginForm: state => {
      return state.login_form
    },
    // 加工 bookList
    showBookList: state => {
      return state.bookList
    },
    // 加工 tableList
    showTableList: state => {
      return state.tableList
    },
    // 加工 bookForm
    showBookForm: state => {
      return state.bookForm
    },
    // 加工 clazz
    showClazz: state => {
      return state.clazz
    },
    // 加工 logName
    showLogName: state => {
      return state.logName
    },
    // 加工 showOrHideBool
    showShowOrHideBool: state => {
      return state.showOrHideBool
    },
    // 加工 uploadImg
    showUploadImg: state => {
      return state.uploadImg
    },
    // 加工 loginUser
    showLoginUser: state => {
      return state.loginUser
    },
    // 加工 userAll
    showUserAll: state => {
      return state.userAll
    },
    // 加工 bookSum
    showBookSum: state => {
      return state.bookSum
    }
  },
  modules: {
  }
})
